package net.wds.ssbus.modules.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wds.ssbus.common.utils.PageUtils;
import net.wds.ssbus.common.utils.Query;
import net.wds.ssbus.modules.dict.entity.TbBaseDictEntity;
import net.wds.ssbus.modules.dict.service.TbBaseDictService;
import net.wds.ssbus.modules.dict.service.TbBaseDictitemService;
import net.wds.ssbus.modules.dict.dao.TbBaseDictitemDao;
import net.wds.ssbus.modules.dict.entity.TbBaseDictitemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service("tbBaseDictitemService")
public class TbBaseDictitemServiceImpl extends ServiceImpl<TbBaseDictitemDao, TbBaseDictitemEntity> implements TbBaseDictitemService {

    @Autowired
    TbBaseDictService tbBaseDictService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TbBaseDictitemEntity> page = this.page(
                new Query<TbBaseDictitemEntity>().getPage(params),
                new QueryWrapper<TbBaseDictitemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getChildren(Map<String, Object> params, Integer id) {
        QueryWrapper<TbBaseDictitemEntity> queryWrapper = new QueryWrapper<TbBaseDictitemEntity>().eq("parent_id", id);
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and(wrapper -> {
                wrapper.like("description",key);
            });
        }
        IPage<TbBaseDictitemEntity> page = this.page(
                new Query<TbBaseDictitemEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void saveTb(TbBaseDictitemEntity tbBaseDictitem) {
        TbBaseDictEntity dictEntity = tbBaseDictService.getById(tbBaseDictitem.getParentId());
//        System.out.println(tbBaseDictitem.getParentId());
        tbBaseDictitem.setDictCode(dictEntity.getCode());
        this.save(tbBaseDictitem);
    }
}